package com.itheima.canal.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration   // 声明当前类是一个配置类
public class RabbitMQConfig {



    //定义交换机名称
    public  static final String Goods_UP_EXCHANGE="goods_up_exchange";

    public static final String GOODS_DOWN_EXCHANGE= "goods_down_exchange";


    //定义队列名称
    public static final String AD_UPDATE_QUEUE="ad_update_queue";

    public static final String SEARCH_ADD_QUEUE= "search_add_queue";

    public static final String SEARCH_DEL_QUEUE="search_del_queue";

    public static final String PAGE_CREATE_QUEUE="page_create_queue";



    //声明队列
    @Bean
    public Queue queue(){
        return new Queue(AD_UPDATE_QUEUE);
    }

    //声明队列
    @Bean(SEARCH_ADD_QUEUE)
    public Queue search_add_queue(){
        return new Queue(SEARCH_ADD_QUEUE);
    }

    //声明队列
    @Bean(SEARCH_DEL_QUEUE)
    public Queue search_del_queue(){
        return new Queue(SEARCH_DEL_QUEUE);
    }

    //声明队列
    @Bean(PAGE_CREATE_QUEUE)
    public Queue page_create_queue(){
        return new Queue(PAGE_CREATE_QUEUE);
    }




    //声明交换机
    @Bean(Goods_UP_EXCHANGE)
    public Exchange goods_up_exchange(){
        /**Goods_UP_EXCHANGE： 交换机名称
         *durableP:  开启持久化
         */
        return ExchangeBuilder.fanoutExchange(Goods_UP_EXCHANGE).durable(true).build();
    }


    //声明交换机
    @Bean(GOODS_DOWN_EXCHANGE)
    public Exchange goods_down_exchange(){
        return ExchangeBuilder.fanoutExchange(GOODS_DOWN_EXCHANGE).durable(true).build();
    }


    //队列与交换机的绑定
    //   Qualifier 取用的bean的别名
    @Bean
    public Binding goods_up_exchange_binding(@Qualifier(SEARCH_ADD_QUEUE)Queue queue,@Qualifier(Goods_UP_EXCHANGE)Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();

    }


    //binding绑定交换机和队列
    @Bean
    public Binding goods_down_exchange_binding(@Qualifier(SEARCH_DEL_QUEUE)Queue queue,@Qualifier(GOODS_DOWN_EXCHANGE)Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();

    }


    //binding绑定交换机和队列
    @Bean
    public Binding page_create_queue_binding(@Qualifier(PAGE_CREATE_QUEUE)Queue queue,@Qualifier(Goods_UP_EXCHANGE)Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();

    }

}
